package blog.service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import blog.entity.Article;
import blog.entity.Category;
import blog.mapper.ArticleCateMapper;
import blog.mapper.ArticleMapper;

/**
 * 文章Servie实现
 */
@Service
public class ArticleServiceImpl implements ArticleService {
	@Resource
	private ArticleMapper articleMapper;

	@Resource
	private ArticleCateMapper articleCateMapper;
	

	/**
	 * 分页查询文章列表(同时把某个文章所属哪些分类也查出来了) 要注意,实现分页查询,需要在mybatis的主配置文件中配置分页插件
	 */
	public PageInfo<Article> getPageArticleList(Integer pageIndex, Integer pageSize, HashMap<String, Object> paramMap) {
		PageHelper.startPage(pageIndex, pageSize);
		List<Article> articleList = articleMapper.findAll(paramMap);

		for (int i = 0; i < articleList.size(); i++) {
			// 封装CategoryList
			List<Category> categoryList = articleCateMapper.listCategoryByArticleId(articleList.get(i).getArticleId());
			if (categoryList == null || categoryList.size() == 0) {
				categoryList = new ArrayList<>();
				categoryList.add(Category.getDefault());
			}
			articleList.get(i).setCategoryList(categoryList);
		}

		return new PageInfo<Article>(articleList);
	}
	
}
